# -*- coding: utf-8 -*-
# find the line of containing chinese in files
 
 
import re
import os

def log(content):
    file_name = "log.txt"
    f = open(file_name, 'a+', encoding='utf-8')
    f.write(content)
    f.close()
    print(content)
 
def find_chinese(file_name):
    find_count = 0
    with open(file_name, 'rb') as infile:
        line = 0
        while True:
            content = infile.readline()
            line = line + 1
            if re.match(r'(.*[\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+.*)', content.decode('utf-8')):
                if content.decode('utf-8').strip().startswith("//") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("*") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("/*") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("<!--") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("Log.") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("log.debug") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("com.vistores.core.utils.Log.") :
                    print("Skip:" + content.decode('utf-8'))
                elif content.decode('utf-8').strip().startswith("System.out.print") :
                    print("Skip:" + content.decode('utf-8'))
                else :
                    log("Line:" + str(line) + " " + content.decode('utf-8').strip() + "\n")
                    find_count += 1
 
            if not content:
                return find_count
    return find_count

def find_project(prj_dir):
    for root, dirs, files in os.walk(prj_dir, topdown=False):
        for name in files:
            if(name.endswith(".xml")) :
                count = find_chinese(os.path.join(root, name))
                if count > 0 :
                    log("File:" + os.path.join(root, name) + "\n\n\n")
                
            if(name.endswith(".java")) :
                count = find_chinese(os.path.join(root, name))
                if count > 0 :
                    log("File:" + os.path.join(root, name) + "\n\n\n")
            if(name.endswith(".kt")) :
                count = find_chinese(os.path.join(root, name))
                if count > 0 :
                    log("File:" + os.path.join(root, name) + "\n\n\n")
        # for name in dirs:
        #     log(os.path.join(root, name))
 
# start to find
if __name__ == '__main__':
    find_project("C:\\workspace\\gwt\\android\\vs\\app\\")
    find_project("C:\\workspace\\gwt\\android\\vs\\core\\")
    find_project("C:\\workspace\\gwt\\android\\vs\\libamqp\\")
    #print("find complete! count =", count)
